package com.zssj.servise.impl;

import com.zssj.common.utils.JWTUtils;
import com.zssj.enums.DeletedEnum;
import com.zssj.framework.exception.ApiException;
import com.zssj.framework.service.impl.BaseServiceImpl;
import com.zssj.mapper.SysUserMapper;
import com.zssj.model.dto.TokenDTO;
import com.zssj.model.entity.SysUser;
import com.zssj.servise.ISysUserService;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.stereotype.Service;

/**
 * @项目名: rzccc_home
 * @描述: 系统用户表 服务类
 * @作者: 杜文骄
 * @时间: 2020-10-02 10:46
 **/
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper,SysUser> implements ISysUserService  {
    @Override
    public SysUser login(String loginName, String passWord) {
        SysUser user = query().eq(SysUser::getLoginName,loginName)
                .eq(SysUser::getDeleted, DeletedEnum.UN_DELETED).getOne();
        System.out.println(Md5Crypt.apr1Crypt(passWord,loginName));
        if (user == null) throw new ApiException("用户信息不存在！");
        if(!Md5Crypt.apr1Crypt(passWord, loginName).equals(user.getPassword()))  throw new ApiException("密码输入有误!");
        if (user.getDeleted() == DeletedEnum.DELETED) throw new ApiException("用户已删除！");
        return user;
    }

    @Override
    public TokenDTO getToken(SysUser user) {
        Long id = user.getId();
        TokenDTO tokenDTO = new TokenDTO();
        tokenDTO.setUid(id);
        tokenDTO.setToken(JWTUtils.generate(id,user.getPostId()));
        return tokenDTO;
    }

}
